using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// FrameBox control provides a simple box surface (descendent of PanelBox), in which is 
	/// rendered an inline frame.
	/// </summary>
	[ParseChildren(false)]
	public class FrameBox : PanelBox
	{
		#region Properties
		/// <summary>
		/// Set to true or false to allow scrolling. If true, the frame
		/// is set to use scrollbars "auto". Unless it's IE, which has a known bug
		/// concerning auto - tables with a width of 100% will calculate their width
		/// as the width of the iframe less the width of the vertical scroll bar, regardless
		/// of whether that scrollbar is visible or not. For this reason, if AllowScroll
		/// is true, scrollbars are turned ON by default in IE.
		/// </summary>
		public bool AllowScroll
		{
			get
			{
				object ascroll = ViewState["as"];
				if( ascroll == null ) return true;
				return (bool) ascroll;
			}
			set
			{
				ViewState["as"] = value;
			}
		}
		/// <summary>
		/// Get/set the URL that is loaded into this FrameBox
		/// </summary>
		public string URL
		{
			get
			{
				string ur = (string) ViewState["ur"];
				return ur == null ? String.Empty : ur;
			}
			set
			{
				ViewState["ur"] = value;
			}
		}
		#endregion
		
		/// <summary>
		/// Renders the opening of the main content row. 
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderCenterRowStart( HtmlTextWriter writer )
		{
			base.RenderCenterRowStart( writer );
			
			string scrolling = "auto";
			if( ! this.AllowScroll ) scrolling = "no";
			else
				if( Page.Request.Browser.Browser == "IE" ) scrolling = "yes";

			writer.Write("<iframe id=\"{0}\" name=\"{0}\" framepadding=\"0\" frameborder=\"0\" scrolling=\"{2}\" noresize=\"true\" src=\"{1}\" style=\"height:100%;width:100%;border:none;\"></iframe>",
				(this.ClientID + "Frame"), Page.ResolveUrl(this.URL), scrolling);
		}
		/// <summary>
		/// Tag end rendering - closes off the panel, we've added code to output
		/// the script function to create a javascript __FrameBox object.
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderEndTag(HtmlTextWriter writer)
		{
			base.RenderEndTag (writer);
			writer.Write("<script language='javascript'>");
			writer.Write("var {0}JS = new __FrameBox('{0}');", this.ClientID);
			writer.Write("</script>");
		}


		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			this.LoadClientScript("FrameBox/FrameBox.js", "Deepcode.Flux.Core.UI.Controls.FrameBox");
		}

	}
}
